package com.util;

import net.sf.json.JSON;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Module:       HttpClientUtil.java
 * Description:  以get/post的方式发送数据到指定的http接口---利用httpclient.jar包---HTTP接口的调用
 * Company:
 * Author:       ptp
 * Date:         Feb 22, 2012
 */
public class HttpClientUtil {

    private static final Logger logger = Logger.getLogger(HttpClientUtil.class);

    private String url;

    /**
     * post方式
     *
     * @return
     */
    public String postHttp(String pallet_id, String shipAddress) {
        String responseMsg = "";

        //1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();

        httpClient.getParams().setContentCharset("UTF-8");

        //2.构造PostMethod的实例
        PostMethod postMethod = new PostMethod(url);

        //3.把参数值放入到PostMethod对象中
        postMethod.addParameter("palletId", pallet_id);
        if (StringUtils.isNotBlank(shipAddress)) {
            postMethod.addParameter("shipAddress", shipAddress);
        }

        try {
            // 4.执行postMethod,调用http接口
            httpClient.executeMethod(postMethod);//200

            //5.读取内容
            responseMsg = postMethod.getResponseBodyAsString().trim();
            logger.info(responseMsg);

            //6.处理返回的内容

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //7.释放连接
            postMethod.releaseConnection();
        }
        return responseMsg;
    }

    public String postHttp(String userName,
                           String key,
                           String model,
                           String boxId,
                           Integer prdQty) {
        String responseMsg = "";

        //1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();

        httpClient.getParams().setContentCharset("UTF-8");

        //2.构造PostMethod的实例
        PostMethod postMethod = new PostMethod(url);

        //3.把参数值放入到PostMethod对象中
        postMethod.addParameter("userName", userName);
        postMethod.addParameter("key", key);
        postMethod.addParameter("model", model);
        postMethod.addParameter("boxId", boxId);
        postMethod.addParameter("prdQty", String.valueOf(prdQty));

        try {
            // 4.执行postMethod,调用http接口
            httpClient.executeMethod(postMethod);//200

            //5.读取内容
            responseMsg = postMethod.getResponseBodyAsString().trim();
            logger.info(responseMsg);

            //6.处理返回的内容

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //7.释放连接
            postMethod.releaseConnection();
        }
        return responseMsg;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


}